﻿using System;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using YArchitech.LIB;

namespace YJKCopyModel
{
	public class ToleranceManger
	{
		private ToleranceManger()
		{
			this.LoadFromFile();
		}

		~ToleranceManger()
		{
			this.SaveToFile();
		}

		public static ToleranceManger GetToleranceManger()
		{
			bool flag = ToleranceManger.m_useSingleTon == null;
			if (flag)
			{
				ToleranceManger.m_useSingleTon = new ToleranceManger();
			}
			return ToleranceManger.m_useSingleTon;
		}

		public double GetTolerance(string inputName = null)
		{
			bool flag = string.IsNullOrWhiteSpace(inputName);
			double result;
			if (flag)
			{
				result = 1E-09;
			}
			else
			{
				bool flag2 = !this.m_useDic.ContainsKey(inputName);
				if (flag2)
				{
					this.m_useDic.Add(inputName, 1E-09);
				}
				result = this.m_useDic[inputName];
			}
			return result;
		}

		[Conditional("DEBUG")]
		public void SetTolerance(string inputName, double inputValue)
		{
			bool flag = string.IsNullOrWhiteSpace(inputName);
			if (!flag)
			{
				bool flag2 = !this.m_useDic.ContainsKey(inputName);
				if (flag2)
				{
					this.m_useDic.Add(inputName, inputValue);
				}
				else
				{
					this.m_useDic[inputName] = inputValue;
				}
			}
		}

		private void LoadFromFile()
		{
			bool flag = File.Exists(ToleranceManger.m_usePath);
			if (flag)
			{
				try
				{
					XmlSerializer xmlSerializer = new XmlSerializer(typeof(ToleranceDic));
					this.m_useDic = (xmlSerializer.Deserialize(File.Open(ToleranceManger.m_usePath, FileMode.Open)) as ToleranceDic);
				}
				catch (Exception)
				{
					this.m_useDic = null;
				}
			}
			bool flag2 = this.m_useDic == null;
			if (flag2)
			{
				this.m_useDic = new ToleranceDic();
			}
		}

		private void SaveToFile()
		{
			try
			{
				bool flag = File.Exists(ToleranceManger.m_usePath);
				if (flag)
				{
					File.Delete(ToleranceManger.m_usePath);
				}
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(ToleranceDic));
				xmlSerializer.Serialize(File.Create(ToleranceManger.m_usePath), this.m_useDic);
			}
			catch (Exception)
			{
			}
		}

		private const double m_default = 1E-09;

		private ToleranceDic m_useDic = null;

		private static string m_usePath = Product.DataLocation + "\\QuickModelingData\\tolerance.xml";

		private static ToleranceManger m_useSingleTon = null;
	}
}
